package com.xh.one2one.lesson.controller;

import com.xh.bussiness.permission.model.User;
import com.xh.bussiness.permission.service.IUserService;
import com.xh.bussiness.thirdapi.xinzhou.model.XzTeacherInfo;
import com.xh.bussiness.thirdapi.xinzhou.service.IXinzhouTmsService;
import com.xh.core.util.UserUtils;
import com.xh.one2one.lesson.model.LessonPlan;
import com.xh.one2one.lesson.service.ILessonInfoService;
import com.xh.one2one.lesson.model.LessonInfo;
import com.xh.one2one.lesson.bean.request.LessonInfoRequest;
import com.xh.core.bean.XhExportHeaderBean;
import com.xh.one2one.lesson.service.ILessonPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import org.springframework.validation.annotation.Validated;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.xh.core.controller.BaseController;

/**
 * <p>
 * 一对一备课 前端控制器
 * </p>
 *
 * @author tw
 * @since 2021-04-07
 */
@RestController
@RequestMapping("/admin/lessonInfo")
public class LessonInfoController extends BaseController {

    @Autowired
    private ILessonInfoService lessonInfoService;
    @Autowired
    private IXinzhouTmsService xinzhouTmsService;
    @Autowired
    private IUserService userService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryPage")
    public AjaxResult queryPage(XhPage page, LessonInfoRequest bean){
        page=lessonInfoService.queryLessonInfoPage(page,bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",page);
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/query")
    public AjaxResult query(LessonInfoRequest bean,HttpServletRequest request){
        XhPage page = new XhPage();
//        String currentUserId = UserUtils.getCurrentUserId();
//        User user = userService.getById(currentUserId);
//        String mobile = user.getMobile();
//        List<XzTeacherInfo> teacherByMobiles = xinzhouTmsService.getTeacherByMobile(mobile);
//        if(CollectionUtils.isEmpty(teacherByMobiles)){
//            return new AjaxResult(AjaxResultConstant.Result.ERROR,"教务系统未查到该用户信息，请检查后再重试!",page);
//        }
//        XzTeacherInfo teacherByMobile = teacherByMobiles.get(0);
        bean.setUserId(UserUtils.getCurrentUserId());
        List list = lessonInfoService.queryLessonInfo(bean);
        page.setRecords(list);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, page);
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryLessons")
    public AjaxResult queryLessons(LessonInfoRequest bean){
        if(null == bean.getSearchType()){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"参数错误！");
        }
        bean.setUserId(UserUtils.getCurrentUserId());
        try {
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",lessonInfoService.queryLessons(bean));
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"查询失败");
        }
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryStudentOrGroups")
    public AjaxResult queryStudentOrGroups(LessonInfoRequest bean,HttpServletRequest request){
        XhPage page = new XhPage();
        String currentUserId = UserUtils.getCurrentUserId();
        User user = userService.getById(currentUserId);
        String mobile = user.getMobile();
        List<XzTeacherInfo> teacherByMobiles = xinzhouTmsService.getTeacherByMobile(mobile);
        if(CollectionUtils.isEmpty(teacherByMobiles)){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"教务系统未查到该用户信息，请检查后再重试!",page);
        }
        XzTeacherInfo teacherByMobile = teacherByMobiles.get(0);
        bean.setUserId(teacherByMobile.getUserId());
        List list = null;
        try {
            list = lessonInfoService.queryStudentOrGroups(bean);
        } catch (Exception e) {
            e.printStackTrace();
        }

        page.setRecords(list);
//        page.setRecords(Lists.newArrayList());
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",page);
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryStudent")
    public AjaxResult queryStudent(LessonInfoRequest bean){
        if (StringUtils.isEmpty(bean.getLessonId())){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"参数错误，请连续管理员！");
        }

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",lessonInfoService.queryStudent(bean));
    }


    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryLessonAndStudent")
    public AjaxResult queryLessonAndStudent(LessonInfoRequest bean){
        bean.setUserId(UserUtils.getCurrentUserId());
        try {
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",lessonInfoService.queryLessonAndStudent(bean));
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"查询失败");
        }

    }
    /**
     * 导出全部
     * @param bean
     * @return
     */
    @PostMapping(value="/exportAll")
    public AjaxResult exportAll(LessonInfoRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=lessonInfoService.exportAll(request,exportHeaderBeans,bean,"queryLessonInfoPage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     * @param lessonInfo 实体对象
     * @return
     */
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated LessonInfo lessonInfo){
        if(lessonInfo.getId()==null){
            lessonInfoService.insert(lessonInfo);
        }else{
            lessonInfoService.update(lessonInfo);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        lessonInfoService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        lessonInfoService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 查询学生评价
     * @author 赵文博
     * @date 2021/6/16 16:36
     * @param page
     * @param bean
     * @return com.xh.core.bean.AjaxResult
     **/
    @PostMapping("/queryStudentEvaluate")
    public AjaxResult queryStudentEvaluate(XhPage page,LessonInfoRequest bean){
        XhPage xhPage = lessonInfoService.queryStudentEvaluate(page,bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",xhPage);
    }
}

